package com.sqgxy.dao.impl;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.sqgxy.pojo.po.FileDate;
import com.sqgxy.dao.FileDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

@Repository
public class FileDaoImpl implements FileDao {
    protected final DataSource dataSource;
    protected final JdbcTemplate jdbcTemplate;

    public FileDaoImpl(DataSource dataSource) {
        this.dataSource = dataSource;
        jdbcTemplate = new JdbcTemplate(dataSource);
    }

    @Override
    public boolean save(FileDate fileDate) {
        List<Object> args = new ArrayList<>();
        args.add(fileDate.getName());
        args.add(fileDate.getType());
        args.add(fileDate.getSize());
        args.add(fileDate.getPath());
        args.add(fileDate.getUuid());
        String sql = "INSERT INTO file_date(name, type, size, path, uuid) VALUES( ?, ?, ?, ?, ?)";
        return jdbcTemplate.update(sql, args.toArray()) > 0;
    }

    @Override
    public boolean delete(String uuid) {
        return jdbcTemplate.update("DELETE FROM file_date WHERE uuid = ?", uuid) > 0;
    }

    @Override
    public FileDate get(String uuid) {
        return jdbcTemplate.queryForObject("SELECT * FROM file_date WHERE uuid = ?", new BeanPropertyRowMapper<>(FileDate.class), uuid);
    }
}
